package com.medical.exception;

import com.medical.domain.R;
import com.medical.enums.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author TISNIW
 * @version 1.0
 * @date 2024/9/20 10:40
 * @desc
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    // 捕获自定义业务异常
    @ExceptionHandler(BusinessException.class)
    public R<String> handleBusinessException(BusinessException e) {
        return R.fail(e.getR().getCode(), e.getR().getMsg());
    }

    // 捕获其他未知异常
    @ExceptionHandler(Exception.class)
    public R<String> handleOtherException(Exception e) {
        e.printStackTrace();
        return R.fail(ResultCode.FAIL.getCode(), "系统异常：" + e.getMessage());
    }
}
